﻿using System;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Net;
using SharpPcap;
using PacketDotNet.Utils;

namespace Bouyei.NetworkSniffer
{
    public delegate void CaptureDataEventArgs(RawCapture rawCapture);

    public delegate void CaptureCommDataEventArgs(UnitPacket commPacket);

    public class CaptureInfo
    {
        public ICaptureDevice CaptureDevice;
        public string CaptureDisplayName;
        public int CaptureId;
    }

    public enum DataPacketType
    {
        Tcp,
        Udp,
        Icmp,
        Icmpv6,
        Igmp,
        Arp,
        /// <summary>
        /// 局域网唤醒协议
        /// </summary>
        WOL,
        /// <summary>
        /// PPP协议
        /// </summary>
        PPP,
        /// <summary>
        /// PPPoE会话阶段协议
        /// </summary>
        PPPoE_S,
        /// <summary>
        /// PPPoE发现阶段协议
        /// </summary>
        PPPoE_D,
        /// <summary>
        /// 无线网络协议
        /// </summary>
        IEEE,
        /// <summary>
        /// 未知或位解析的协议
        /// </summary>
        Unkown
    }

    public class LinkFilter
    {
        public bool IsEthernet = true;
        public bool IsIeee = true;
        public bool IsPPP = true;

        private bool isAll = true;
        public bool IsAll {
            get { return isAll; }
            set { isAll = value;
            if (value)
            {
                IsEthernet = true;
                IsIeee = true;
                IsPPP = true;
            }
            }
        }
    }

    public class UnitPacket
    {
        public static long Id { get; private set; }

        public long SNo { get; private set; }

        private DataPacketType dpType = DataPacketType.Unkown;
        public DataPacketType pType {
            get { return dpType; }
            set { dpType = value; }
        }

        public IPAddress SourceAddress { get; set; }

        public IPAddress DestinationAddress { get; set; }

        public PhysicalAddress SourceMacAddress { get; set; }

        public PhysicalAddress DestinationMacAddress { get; set; }

        public UInt16 SourcePort { get; set; }

        public UInt16 DestinactionPort { get; set; }

        public byte[] pBuffer { get; set; }

        public ByteArraySegment rawBuffer { get; set; }

        public string OtherContent { get; set; }

        public UnitPacket()
        {
            Id++;
            if (Id == long.MaxValue)
            {
                Id = 0;
            }
            SNo = Id;
        }
    }
}
